package com.xxx.car.web.admin;

import com.github.pagehelper.PageInfo;
import com.xxx.car.entity.TbInvoice;
import com.xxx.car.entity.vo.request.ReqInvoiceAdminQuery;
import com.xxx.car.entity.vo.request.ReqInvoiceQuery;
import com.xxx.car.entity.vo.request.ReqInvoiceUpdate;
import com.xxx.car.entity.vo.response.RespInvoiceDetail;
import com.xxx.car.service.InvoiceService;
import com.xxx.common.base.vo.APIResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by yxj on 17/6/26.
 */
@Controller
@RequestMapping("/admin/invoice")
public class AdminInvoiceController {

	@Autowired
	InvoiceService invoiceService;

	@RequestMapping(value = "/")
	public String invoiceList(Model model, @ModelAttribute ReqInvoiceAdminQuery query) {
		PageInfo pageInfo = invoiceService.queryInvoiceAdminList(query);
		Integer completeCount = invoiceService.countCompleteInvoice();
		Integer unCompleteCount = (int)pageInfo.getTotal() -completeCount;
		//获得当前页
		model.addAttribute("pageNum", pageInfo.getPageNum());
		//获得一页显示的条数
		model.addAttribute("pageSize", pageInfo.getPageSize());
		//是否是第一页
		model.addAttribute("isFirstPage", pageInfo.isIsFirstPage());
		//获得总页数
		model.addAttribute("totalPages", pageInfo.getPages());
		//是否是最后一页
		model.addAttribute("isLastPage", pageInfo.isIsLastPage());
		//前台对象
		model.addAttribute("invoiceList", pageInfo.getList());
		model.addAttribute("completeCount", completeCount);
		model.addAttribute("unCompleteCount", unCompleteCount);
		//查询条件
		model.addAttribute("query",query);
		return "invoiceList";
	}

	@RequestMapping(value = "/detail")
	public String invoiceDetail(Model model, @ModelAttribute ReqInvoiceQuery query) {
		RespInvoiceDetail respInvoiceDetail = invoiceService.queryInvoiceDetail(query);
		model.addAttribute("respInvoiceDetail", respInvoiceDetail);
		return "invoiceDetail";
	}

	@RequestMapping(value = "/insertReply")
	public String insertReply(Model model, @ModelAttribute ReqInvoiceUpdate update) {
		APIResponse response = invoiceService.updateInvoiceReply(update);
		model.addAttribute("msg", response.getData());
		return "redirect:/admin/invoice/detail/?id="+update.getInvoiceId();
	}

}
